home *** CD-ROM | disk | FTP | other *** search
/ Hyper Stacks 1994 May / Hyper Stacks (Pacific HiTech)(1994)[Mac].iso / HyperTalk / CopyFile XFCN / CopyFile.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1988-12-16  |  5.4 KB  |  168 lines  |  [TEXT/TPAS]

  1. program CopyFile;
  2. {
  3.   This XFCN will copy the file named in params[1] into a new file in
  4.   params[2].  Both filenames need to be complete path names to
  5.   guarantee success.  In the event of failure, it returns the standard
  6.   Macintosh error code.
  7.  
  8.   No warranty is made for this code, at all.  It has worked for me so far,
  9.   and that's all I can say.  If you spot a bug, please drop me a note via
  10.   FidoNet network mail to 1:100/523 (or on the EchoMac or MacDev echomail
  11.   conferences), or via Compu$erve EasyPlex to [76012,300], or via U.S.
  12.   Snail to J. Brad Hicks, 11215 Sugartrail, St. Louis, MO 63136 (in that order
  13.   of preference).
  14.   
  15.   CopyFile was written and compiled in Turbo Pascal / Macintosh version 1.00A.
  16. }
  17. {Remember to change the resource ID to 20517 after compiling}
  18.  
  19. {$R-}
  20. {$U-}
  21. {$D PasXFCN}
  22.  
  23. USES Memtypes,QuickDraw,OSIntf,HyperXCmd;
  24.  
  25. PROCEDURE PasXFCN(paramPtr: XCmdPtr);
  26. {$I Hard Disk:Turbo Pascal:XCMD Folder:XCmdGlue.inc}
  27.  
  28.   LABEL 9940,9950,9960,9970,9980,9985,9990,9999;
  29.  
  30.   CONST
  31.     MaxBuff  = 32000;
  32.     ParamErr =     1;
  33.  
  34.   VAR
  35.     iData, iRF,                    { input fref numbers, data and resource  }
  36.     oData, oRF      : integer;     { output fref numbers, data and resource }
  37.     fileSize,                      { file size in bytes                     }
  38.     i,blocks,                      { block number and number of blocks      }
  39.     bytes           : longint;     { bytes read each operation              }
  40.     p               : ParmBlkPtr;  { parameter block for low-level file i/o }
  41.     iFInfo          : FInfo;       { file finder information, both files    }
  42.     ignore,                        { temporary error code                   }
  43.     errorCode       : OSErr;       { last error code (also result)          }
  44.     iFName,oFName   : Str255;      { full file names, both files            }
  45.     bitBucket       : Ptr;         { pointer to space for the i/o           }
  46.   
  47.   BEGIN
  48.  
  49.     with paramPtr^ do begin
  50.  
  51.       if paramCount <> 2 then
  52.         begin
  53.           errorCode := ParamErr;
  54.           goto 9999
  55.         end;
  56.       ZeroToPas(params[1]^,iFName);
  57.       ZeroToPas(params[2]^,oFName);
  58.  
  59.       bitBucket := NewPtr(MaxBuff);
  60.       errorCode := MemError;
  61.       if errorCode <> noErr then goto 9999;
  62.       p := ParmBlkPtr(NewPtr(sizeof(ParamBlockRec)));
  63.       errorCode := MemError;
  64.       if errorCode <> noErr then goto 9990;
  65.  
  66.       errorCode := GetFInfo(iFName,0,iFInfo);
  67.       if errorCode <> noErr then goto 9985 else begin
  68.  
  69.         errorCode := Create(oFName,0,iFInfo.fdCreator,iFInfo.fdType);
  70.         if errorCode <> noErr then goto 9985;
  71.  
  72.      (* errorCode := FSOpen(iFName,0,iData); *)
  73.         with p^ do begin
  74.           ioCompletion := nil;   { no follow-on routine      }
  75.           ioNamePtr := @iFName;  { pointer to path:file name }
  76.           ioVRefNum := 0;        { dummy volume number       }
  77.           ioVersNum := 0;        { version always = 0        }
  78.           ioPermssn := fsRdPerm; { request read-only         }
  79.           ioMisc    := nil       { use volume i/o buffer     }
  80.         end {with};
  81.         errorCode := PBOpen(p,false);
  82.         if errorCode <> noErr then goto 9980;
  83.         iData := p^.ioRefNum;
  84.  
  85.         errorCode := GetEOF(iData,fileSize);
  86.         if errorCode <> noErr then goto 9970;
  87.  
  88.         if fileSize > 0 then begin
  89.  
  90.           errorCode := FSOpen(oFName,0,oData);
  91.           if errorCode <> noErr then goto 9970;
  92.  
  93.           errorCode := Allocate(oData,fileSize);
  94.           if errorCode <> noErr then goto 9960;
  95.  
  96.           blocks := (fileSize + MaxBuff - 1) div MaxBuff;
  97.           for i := 1 to blocks do begin
  98.  
  99.             bytes := MaxBuff;
  100.             errorCode := FSRead(iData,bytes,bitBucket);
  101.             if (errorCode <> noErr) and (errorCode <> eofErr) then goto 9960;
  102.             errorCode := FSWrite(oData,bytes,bitBucket);
  103.             if errorCode <> noErr then goto 9960
  104.  
  105.           end {for}
  106.  
  107.         end {if};
  108.  
  109.      (* errorCode := OpenRF(iFName,0,iRF); *)
  110.         with p^ do begin
  111.           ioCompletion := nil;   { no follow-on routine      }
  112.           ioNamePtr := @iFName;  { pointer to path:file name }
  113.           ioVRefNum := 0;        { dummy volume number       }
  114.           ioVersNum := 0;        { version always = 0        }
  115.           ioPermssn := fsRdPerm; { request read-only         }
  116.           ioMisc    := nil       { use volume i/o buffer     }
  117.         end {with};
  118.         errorCode := PBOpenRF(p,false);
  119.         if errorCode <> noErr then goto 9960;
  120.         iRF := p^.ioRefNum;
  121.  
  122.         errorCode := GetEOF(iRF,fileSize);
  123.         if errorCode <> noErr then goto 9950;
  124.  
  125.         if fileSize > 0 then begin
  126.  
  127.           errorCode := OpenRF(oFName,0,oRF);
  128.           if errorCode <> noErr then goto 9940;
  129.  
  130.           errorCode := Allocate(oRF,fileSize);
  131.           if errorCode <> noErr then goto 9940;
  132.  
  133.           blocks := (fileSize + MaxBuff - 1) div MaxBuff;
  134.           for i := 1 to blocks do begin
  135.  
  136.             bytes := MaxBuff;
  137.             errorCode := FSRead(iRF,bytes,bitBucket);
  138.             if (errorCode <> noErr) and (errorCode <> eofErr) then goto 9940;
  139.             errorCode := FSWrite(oRF,bytes,bitBucket);
  140.             if errorCode <> noErr then goto 9940
  141.  
  142.           end {for}
  143.  
  144.         end {if}
  145.  
  146.       end {else};
  147.  
  148. 9940: ignore := FSClose(oRF);
  149. 9950: ignore := FSClose(iRF);
  150. 9960: ignore := FSClose(oData);
  151. 9970: ignore := FSClose(iData);
  152. 9980: if errorCode <> noErr then ignore := FSDelete(oFName,0);
  153. 9985: disposPtr(ptr(p));
  154. 9990: disposPtr(bitBucket);
  155.  
  156. 9999: returnValue := PasToZero(NumToStr(errorCode))
  157.  
  158.     end {with}
  159.   
  160.   END;
  161.  
  162. BEGIN
  163. END.
  164.  
  165.  
  166.  
  167.  
  168.